package com.auto.learning;

import java.io.File;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.os.Handler;
import android.os.Message;
import cn.jpush.android.api.BasicPushNotificationBuilder;
import cn.jpush.android.api.JPushInterface;
import cn.sharesdk.framework.ShareSDK;

import com.auto.learning.event.EventBus;
import com.auto.learning.net.HttpRequestHelper;
import com.auto.learning.store.core.ModelCreator;
import com.auto.learning.store.core.ModelFinder;
import com.auto.learning.store.core.StaticReference;
import com.auto.learning.tools.FileOpUtils;
import com.auto.learning.tools.FileUtil;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.tencent.bugly.crashreport.CrashReport;

public class Application extends android.app.Application {
	
	private Handler uiHandler;
	
	@Override
	public void onCreate() {
		super.onCreate();
		initJPushSetting();
		ShareSDK.initSDK(this);
		initStores();
		initConfig();
		initImageLoader();
		initUIHandler();
		
		
//		CrashHandler.getInstance().init(getApplicationContext());
		CrashReport.initCrashReport(getApplicationContext());  // bugly
	}
	
	
	private void initJPushSetting(){
		JPushInterface.setDebugMode(HttpRequestHelper.M_SHOW_LOG);
	    JPushInterface.init(this);
	    BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder(getApplicationContext());
	    builder.statusBarDrawable = R.drawable.icon_small;
	    builder.notificationFlags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;;
	    builder.notificationDefaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE;
	    JPushInterface.setPushNotificationBuilder(1, builder);
	}
	
	/**
	 * 初始化本地数据库
	 */
	private void initStores(){
		StaticReference.auto_db_mc = ModelCreator.build(this);
		StaticReference.auto_db_mf = ModelFinder.build(this);
		StaticReference.mutilDBSupport = StaticReference.auto_db_mf.getMutilDBSupport();
	}
	
	private void initConfig(){
		FileUtil.initFileCachePath(this, "AutoLearning");
		FileOpUtils.copyNecessaryFile(this, "icon.png");
		EventBus.registerApp(this);
	}
	
	@SuppressLint("HandlerLeak")
	private void initUIHandler() {
		if (uiHandler == null) {
			uiHandler = new Handler() {
				@Override
				public void handleMessage(Message msg) {
					super.handleMessage(msg);
				}
			};
		}
	}

	public Handler getUIHandler() {
		return uiHandler;
	}
	/**
	 * 图片缓存
	 */
	public void initLoader() {
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext()).memoryCacheExtraOptions(480, 800)
				.threadPriority(Thread.NORM_PRIORITY - 2).denyCacheImageMultipleSizesInMemory()
				.memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))
				.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
				.imageDownloader(new BaseImageDownloader(getApplicationContext(),5 * 1000, 30 * 1000)) // connectTimeout
				.writeDebugLogs()
				.build();
		ImageLoader.getInstance().init(config);// 全局初始化此配置
	}
	
	
	 //初始化网络图片缓存库
    private void initImageLoader(){
    	File cacheDir =new File(FileUtil.getFileCachePath() + "/cacheImage"); 
        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder().
                showImageForEmptyUri(R.drawable.book)
                .cacheInMemory(true).cacheOnDisk(true).build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                getApplicationContext()).defaultDisplayImageOptions(defaultOptions)
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .memoryCacheSize(2 * 1024 * 1024)     
                .discCacheSize(50 * 1024 * 1024)
                .diskCacheFileNameGenerator(new Md5FileNameGenerator()).discCache(new UnlimitedDiscCache(cacheDir))
                .tasksProcessingOrder(QueueProcessingType.LIFO).memoryCache(new WeakMemoryCache()).build();
        ImageLoader.getInstance().init(config);
    }
    
    private void initImage(){
    	DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
    	.cacheInMemory(true).imageScaleType(ImageScaleType.EXACTLY)
    	.cacheOnDisk(true).build();
    	ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
    	.threadPoolSize(3)
    	// default
    	.threadPriority(Thread.NORM_PRIORITY - 2)
    	.denyCacheImageMultipleSizesInMemory()
    	.diskCacheFileNameGenerator(new Md5FileNameGenerator())
    	.tasksProcessingOrder(QueueProcessingType.LIFO)
    	.denyCacheImageMultipleSizesInMemory()
//    	 .memoryCache(new LruMemoryCache((int) (6 * 1024 * 1024)))
    	.memoryCache(new WeakMemoryCache())
    	.memoryCacheSize((int) (2 * 1024 * 1024))
    	.memoryCacheSizePercentage(13)
    	// default
    	.diskCacheSize(50 * 1024 * 1024).diskCacheFileCount(100)
    	.diskCacheFileNameGenerator(new HashCodeFileNameGenerator())
    	.defaultDisplayImageOptions(defaultOptions)   // Remove
    	.build();
    	// Initialize ImageLoader with configuration.
    	ImageLoader.getInstance().init(config);

    }
    
    
    
}
